package com.zushiye.order.controller;


import com.zushiye.order.model.OrderItemBean;
import com.zushiye.order.model.request.OrderDelRequest;
import com.zushiye.order.model.request.OrderQueryRequest;
import com.zushiye.order.model.request.OrderUpdateRequest;
import com.zushiye.order.model.response.BaseResponse;
import com.zushiye.order.model.response.OrderListResponse;
import com.zushiye.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService service;

    //http://127.0.0.1:8080/cart/add
    @PostMapping(value = "/add")
    public BaseResponse add(@RequestBody OrderItemBean itemBean) {
        BaseResponse response = new BaseResponse();
        if (itemBean.user_id == 0) {
            response.msg = "添加失败";
            return response;
        }
        int i = service.insert(itemBean);
        if (i >= 0) {
            response.msg = "添加成功";
            response.code = 1;
        } else {
            response.msg = "添加失败";
        }
        return response;
    }

    @PostMapping(value = "/delete")
    public BaseResponse delete(@RequestBody OrderDelRequest req) {
        BaseResponse response = new BaseResponse();
        int i = service.delete(req.id);
        if (i >= 0) {
            response.msg = "删除成功";
            response.code = 1;
        } else {
            response.msg = "删除失败";
        }
        return response;
    }

    @PostMapping(value = "/updateState")
    public BaseResponse updateState(@RequestBody OrderUpdateRequest req) {
        BaseResponse response = new BaseResponse();
        int i = service.updateState(req);
        if (i >= 0) {
            response.msg = "修改成功";
            response.code = 1;
        } else {
            response.msg = "修改失败";
        }
        return response;
    }

    @PostMapping(value = "/queryAll")
    public OrderListResponse queryAll(@RequestBody OrderQueryRequest req) {
        OrderListResponse response = new OrderListResponse();
        List<OrderItemBean> list = service.queryAll(req.user_id);
        if (list == null) {
            response.msg = "查询失败";
        } else {
            response.list = list;
            response.msg = "查询成功";
            response.code = 1;
        }
        return response;
    }

}
